<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>JQuery | 静待花开</title><meta name="author" content="brad 随风起舞"><meta name="copyright" content="brad 随风起舞"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="@[toc] JQuery是什么jQuery由美国人John Resig创建，至今已吸引了来自世界各地的众多javascript高手加入其team。其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。它是轻量级的js库(压缩后只有21k) ，还兼容各种浏览器jQuery是一个快速的，简洁的javaScript库，使用户能更方便地处理HTML documents、event">
<meta property="og:type" content="article">
<meta property="og:title" content="JQuery">
<meta property="og:url" content="https://zhangbo4881820.gitee.io/myblog/posts/63260/index.html">
<meta property="og:site_name" content="静待花开">
<meta property="og:description" content="@[toc] JQuery是什么jQuery由美国人John Resig创建，至今已吸引了来自世界各地的众多javascript高手加入其team。其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。它是轻量级的js库(压缩后只有21k) ，还兼容各种浏览器jQuery是一个快速的，简洁的javaScript库，使用户能更方便地处理HTML documents、event">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png">
<meta property="article:published_time" content="2019-10-12T01:40:10.000Z">
<meta property="article:modified_time" content="2023-09-07T09:44:01.877Z">
<meta property="article:author" content="brad 随风起舞">
<meta property="article:tag" content="前端知识">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png"><link rel="shortcut icon" href="/myblog/img/favicon.png"><link rel="canonical" href="https://zhangbo4881820.gitee.io/myblog/posts/63260/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/myblog/css/index.css"><link rel="stylesheet" href="/myblog/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/myblog/css/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/myblog/',
  algolia: undefined,
  localSearch: {"path":"/myblog/search.xml","preload":false,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'JQuery',
  isPost: true,
  isHome: false,
  isHighlightShrink: true,
  isToc: true,
  postUpdate: '2023-09-07 17:44:01'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 7.0.0-rc2"><link rel="alternate" href="/myblog/atom.xml" title="静待花开" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/myblog/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/myblog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a><a href="/myblog/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/myblog/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/myblog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/myblog/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/myblog/" title="静待花开"><span class="site-name">静待花开</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/myblog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分享</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/myblog/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/myblog/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/myblog/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/myblog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/myblog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">JQuery</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-10-12T01:40:10.000Z" title="发表于 2019-10-12 09:40:10">2019-10-12</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-09-07T09:44:01.877Z" title="更新于 2023-09-07 17:44:01">2023-09-07</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/myblog/categories/JQuery/">JQuery</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="JQuery"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><p>@[toc]</p>
<h2 id="JQuery是什么"><a href="#JQuery是什么" class="headerlink" title="JQuery是什么"></a>JQuery是什么</h2><p>jQuery由美国人John Resig创建，至今已吸引了来自世界各地的众多javascript高手加入其team。<br>其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。<br>它是轻量级的js库(压缩后只有21k) ，还兼容各种浏览器<br>jQuery是一个快速的，简洁的javaScript库，使用户能更方便地处理HTML documents、events、实现动画效果，并且方便地为网站提供AJAX交互。<br>jQuery还有一个比较大的优势是，它的文档说明很全，而且各种应用也说得很详细，同时还有许多成熟的插件可供选择。</p>
<h2 id="JQuery的引入"><a href="#JQuery的引入" class="headerlink" title="JQuery的引入"></a>JQuery的引入</h2><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;script type=&quot;text/javascript&quot; src=&quot;script/jquery-1.8.3.js&quot;&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h3 id="加载主函数"><a href="#加载主函数" class="headerlink" title="加载主函数"></a>加载主函数</h3><h4 id="第一种方式"><a href="#第一种方式" class="headerlink" title="第一种方式"></a>第一种方式</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">$(document).ready(function()&#123;</span><br><span class="line">	var div1 = document.getElementById(&quot;div1&quot;);</span><br><span class="line">	alert(div1);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h4 id="第二种方式"><a href="#第二种方式" class="headerlink" title="第二种方式"></a>第二种方式</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">$(function()&#123;</span><br><span class="line">    var div1 = document.getElementById(&quot;div1&quot;)</span><br><span class="line">    alert(div1)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="什么是JQuery对象"><a href="#什么是JQuery对象" class="headerlink" title="什么是JQuery对象"></a>什么是JQuery对象</h2><p>jQuery 对象就是通过jQuery包装DOM对象后产生的对象。<br>jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuery 里的方法。<br>虽然jQuery对象是包装DOM对象后产生的，但是jQuery无法使用DOM对象的任何方法，同理DOM对象也不能使用jQuery里的方法.乱使用会报错</p>
<h3 id="JQuery对象和DOM对象之间的转换"><a href="#JQuery对象和DOM对象之间的转换" class="headerlink" title="JQuery对象和DOM对象之间的转换"></a>JQuery对象和DOM对象之间的转换</h3><p>第一种 <em>DOM对象转成JQuery对象</em><br>对于已经是一个DOM对象，只需要用$()把DOM对象包装起来，就可以获得一个jQuery对象了。$(DOM对象) ;<br>代码如下：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">function myclick()&#123;</span><br><span class="line">	var div1 = document.getElementById(&quot;myinput&quot;);</span><br><span class="line">	//把dom转换成jQuery对象</span><br><span class="line">	var jdiv1 = $(div1);</span><br><span class="line">	alert(jdiv1.val());</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>第二种 <em>JQuery对象转成DOM对象</em><br>代码如下:</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">function myclick()&#123;</span><br><span class="line">    var div1 = document.getElementById(&quot;myinput&quot;);</span><br><span class="line">	//把dom转换成jQuery对象</span><br><span class="line">	var jdiv1 = $(div1);</span><br><span class="line">	//把jQuery对象转换成dom对象</span><br><span class="line">	alert(jdiv1[0].value);</span><br><span class="line">	alert(jdiv1.get(0).value);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="JQuery选择器"><a href="#JQuery选择器" class="headerlink" title="JQuery选择器"></a>JQuery选择器</h2><h3 id="基本选择器"><a href="#基本选择器" class="headerlink" title="基本选择器"></a>基本选择器</h3><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">#id</td>
<td align="left">$(”#myDiv”)</td>
<td align="center">单个元素的组成的集合</td>
<td>直接选择html中的id&#x3D;”myDiv”</td>
</tr>
<tr>
<td align="center">Element</td>
<td align="left">$(”div”)</td>
<td align="center">集合元素</td>
<td>element其实就是html已经定义的标签元素</td>
</tr>
<tr>
<td align="center">class</td>
<td align="left">$(”.myClass”)</td>
<td align="center">集合元素</td>
<td>选择html代码中class&#x3D;”myClass”的元素或元素组</td>
</tr>
</tbody></table>
<ul>
<li>|$(”*”)|集合元素|匹配所有元素<br>selector1, selector2, selectorN|$(”div,span,p.myClass”)|集合元素|每一个选择器匹配到的元素合并后一起返回</li>
</ul>
<h3 id="层次选择器"><a href="#层次选择器" class="headerlink" title="层次选择器"></a>层次选择器</h3><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="left">返回值</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">祖下的后代(ancestor descendant)</td>
<td align="left">$(”form input”)</td>
<td align="left">集合元素</td>
<td>给定的祖先元素下匹配所有后代元素</td>
</tr>
<tr>
<td align="center">父下的子(parent &gt; child)</td>
<td align="left">$(”form &gt; input”)</td>
<td align="left">集合元素</td>
<td>给定的父元素下匹配所有子元素.注意:要区分好后代元素与子元素</td>
</tr>
<tr>
<td align="center">(prev + next)</td>
<td align="left">$(”label + input”)</td>
<td align="left">集合元素</td>
<td>匹配所有紧接在prev元素后的next元素</td>
</tr>
<tr>
<td align="center">(prev ~ siblings)</td>
<td align="left">$(”form ~ input”)</td>
<td align="left">集合元素</td>
<td>匹配prev元素之后的所有siblings元素,兄弟辈的,也就是说匹配的是和prev同辈元素</td>
</tr>
</tbody></table>
<h3 id="过滤选择器"><a href="#过滤选择器" class="headerlink" title="过滤选择器"></a>过滤选择器</h3><p><em>过滤选择器主要是通过特定的过滤规则来筛选出所需的 DOM 元素, 该选择器都以 “:” 开头</em></p>
<h4 id="基本过滤选择器"><a href="#基本过滤选择器" class="headerlink" title="基本过滤选择器"></a>基本过滤选择器</h4><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th align="right">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">:first</td>
<td align="left">$(”tr:first”)</td>
<td align="center">单个元素的组成的集合</td>
<td align="right">匹配找到的第一个元素(第一个tr)</td>
</tr>
<tr>
<td align="center">:last</td>
<td align="left">$(”tr:last”)</td>
<td align="center">单个元素的组成的集合</td>
<td align="right">匹配找到的最后一个元素.(最后一个tr)</td>
</tr>
<tr>
<td align="center">:not(selector)</td>
<td align="left">$(”input:not(:checked)”)</td>
<td align="center">集合元素</td>
<td align="right">意思是没有被选中的input(当input的type&#x3D;”checkbox”)</td>
</tr>
<tr>
<td align="center">:even</td>
<td align="left">$(”tr:even”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有索引值为偶数的元素(table中的第一个tr就为偶数0)</td>
</tr>
<tr>
<td align="center">:odd</td>
<td align="left">$(”tr:odd”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有索引值为奇数的元素</td>
</tr>
<tr>
<td align="center">:eq(index)</td>
<td align="left">$(”tr:eq(0)”)</td>
<td align="center">集合元素</td>
<td align="right">匹配一个给定索引值的元素(eq(0)就是获取第一个tr元素)</td>
</tr>
<tr>
<td align="center">:gt(index)</td>
<td align="left">$(”tr:gt(0)”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有大于给定索引值的元素</td>
</tr>
<tr>
<td align="center">:lt(index)</td>
<td align="left">$(”tr:lt(2)”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有小于给定索引值的元素.</td>
</tr>
<tr>
<td align="center">:header(固定写法)</td>
<td align="left">$(”:header”).css(”background”,“#EEE”)</td>
<td align="center">集合元素</td>
<td align="right">匹配如 h1, h2之类的标题元素,这个是专门用来获取h1,h2这样的标题元素</td>
</tr>
<tr>
<td align="center">:animated(固定写法)</td>
<td align="left">$(”:animated”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有正在执行动画效果的元素</td>
</tr>
</tbody></table>
<h4 id="属性过滤选择器"><a href="#属性过滤选择器" class="headerlink" title="属性过滤选择器"></a>属性过滤选择器</h4><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th align="right">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">[attribute]</td>
<td align="left">$(”div[id]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配包含给定属性的元素(例子选取的是“id”属性的div标签)</td>
</tr>
<tr>
<td align="center">[attribute&#x3D;value]</td>
<td align="left">$(”input[name&#x3D;’newsletter’]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配给定的属性是某个特定值的元素</td>
</tr>
<tr>
<td align="center">[attribute!&#x3D;value]</td>
<td align="left">$(”input[name!&#x3D;’newsletter’]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有不含有指定的属性,或者属性不等于特定值的元素</td>
</tr>
<tr>
<td align="center">[attribute^&#x3D;value]</td>
<td align="left">$(”input[name^&#x3D;‘news’]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配给定的属性是以某些值开始的元素</td>
</tr>
<tr>
<td align="center">[attribute$&#x3D;value]</td>
<td align="left">$(”input[name$&#x3D;‘letter’]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配给定的属性是以某些值结尾的元素</td>
</tr>
<tr>
<td align="center">[attribute*&#x3D;value]</td>
<td align="left">$(”input[name*&#x3D;‘man’]“)</td>
<td align="center">集合元素</td>
<td align="right">匹配给定的属性是以包含某些值的元素</td>
</tr>
<tr>
<td align="center">[attributeFilter1][attributeFilterN]</td>
<td align="left">$(”input[id][name$&#x3D;‘man’]“)</td>
<td align="center">集合元素</td>
<td align="right">复合属性选择器,需要同时满足多个条件时使用</td>
</tr>
</tbody></table>
<h4 id="子元素过滤选择器"><a href="#子元素过滤选择器" class="headerlink" title="子元素过滤选择器"></a>子元素过滤选择器</h4><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th align="right">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">:nth-child(index)</td>
<td align="left">$(”ul li:nth-child(2)”)</td>
<td align="center">集合元素</td>
<td align="right">匹配其父元素下的第N个子或奇偶元素(本例种取得是第二个子元素),这个索引是从1开始</td>
</tr>
<tr>
<td align="center">:first-child</td>
<td align="left">$(”ul li:first-child”)</td>
<td align="center">集合元素</td>
<td align="right">匹配第一个子元素(本例代表取每个ul下的第一个子元素)</td>
</tr>
<tr>
<td align="center">:last-child</td>
<td align="left">$(”ul li:last-child”)</td>
<td align="center">集合元素</td>
<td align="right">匹配每个父下的最后一个子元素</td>
</tr>
<tr>
<td align="center">:only-child</td>
<td align="left">$(”ul li:only-child”)</td>
<td align="center">集合元素</td>
<td align="right">如果某个元素是父元素中唯一的子元素,那将会被匹配:(意思是只有一个子元素才匹配)</td>
</tr>
</tbody></table>
<h4 id="表单对象属性过滤选择器"><a href="#表单对象属性过滤选择器" class="headerlink" title="表单对象属性过滤选择器"></a>表单对象属性过滤选择器</h4><p><em>此选择器主要对所选择的表单元素进行过滤</em></p>
<table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th align="right">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">:enabled</td>
<td align="left">$(”input:enabled”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有可用元素(意思是不带有disabled&#x3D;”disabled”的元素)</td>
</tr>
<tr>
<td align="center">:disabled</td>
<td align="left">$(”input:disabled”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有不可用元素(意思是带有disabled&#x3D;”disabled”的元素)</td>
</tr>
<tr>
<td align="center">:checked</td>
<td align="left">$(”input:checked)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有选中的被选中元素(复选框</td>
</tr>
<tr>
<td align="center">:selected</td>
<td align="left">$(”select option:selected”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有选中的option元素</td>
</tr>
</tbody></table>
<h4 id="表单选择器"><a href="#表单选择器" class="headerlink" title="表单选择器"></a>表单选择器</h4><table>
<thead>
<tr>
<th align="center">元素</th>
<th align="left">用法</th>
<th align="center">返回值</th>
<th align="right">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">:input</td>
<td align="left">$(”:input”)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有 input, textarea, select 和 button 元素</td>
</tr>
<tr>
<td align="center">:text</td>
<td align="left">$(”:text)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有的单行文本框</td>
</tr>
<tr>
<td align="center">:password</td>
<td align="left">$(”:password)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有密码框</td>
</tr>
<tr>
<td align="center">:radio</td>
<td align="left">$(”:radio)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有单选按钮</td>
</tr>
<tr>
<td align="center">:checkbox</td>
<td align="left">$(”:checkbox)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有复选框</td>
</tr>
<tr>
<td align="center">:submit</td>
<td align="left">$(”:submit)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有提交按钮</td>
</tr>
<tr>
<td align="center">:image</td>
<td align="left">$(”:image)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有图像域</td>
</tr>
<tr>
<td align="center">:reset</td>
<td align="left">$(”:reset)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有重置按钮</td>
</tr>
<tr>
<td align="center">:button</td>
<td align="left">$(”:button)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有按钮.这个包括直接写的元素button</td>
</tr>
<tr>
<td align="center">:file</td>
<td align="left">$(”:file)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有文件域.</td>
</tr>
<tr>
<td align="center">:hidden</td>
<td align="left">$(”:hidden)</td>
<td align="center">集合元素</td>
<td align="right">匹配所有不可见元素,或者type为hidden的元素,这个选择器就不仅限于表单,那些style为hidden的也会被匹配</td>
</tr>
</tbody></table>
<h2 id="JQuery中的DOM操作"><a href="#JQuery中的DOM操作" class="headerlink" title="JQuery中的DOM操作"></a>JQuery中的DOM操作</h2><p>DOM(Document Object Model—文档对象模型):一种与浏览器,平台,语言无关的接口,使用该接口可以轻松地访问页面中所有的标准组件</p>
<ol>
<li>append(content) :向每个匹配的元素的内部的结尾处追加内容<br><strong>代码示例:</strong><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">function myclick()&#123;</span><br><span class="line">	var len = $(&quot;li&quot;).length;</span><br><span class="line">	//创建li的jQuery元素</span><br><span class="line">	var liObj = $(&quot;&lt;li&gt;列表项&quot;+(len+1)+&quot;&lt;/li&gt;&quot;);</span><br><span class="line">	//在ul内追加jQuery的li元素</span><br><span class="line">	$(&quot;ul&quot;).append(liObj);</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;ul&gt;</span><br><span class="line">	&lt;li&gt;列表项1&lt;/li&gt;</span><br><span class="line">	&lt;li&gt;列表项2&lt;/li&gt;</span><br><span class="line">	&lt;li&gt;列表项3&lt;/li&gt;</span><br><span class="line">	&lt;li&gt;列表项4&lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br></pre></td></tr></table></figure></li>
<li>appendTo(content) :将每个匹配的元素追加到指定的元素中的内部结尾处<br><strong>代码示例:</strong><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">function myclick()&#123;</span><br><span class="line">	var len = $(&quot;li&quot;).length;</span><br><span class="line">	//创建li的jQuery元素</span><br><span class="line">	var liObj = $(&quot;&lt;li&gt;列表项&quot;+(len+1)+&quot;&lt;/li&gt;&quot;);</span><br><span class="line">	//在ul内追加jQuery的li元素</span><br><span class="line">	//$(&quot;ul&quot;).append(liObj);</span><br><span class="line">	var urlObj = $(&quot;ul&quot;);</span><br><span class="line">	liObj.appendTo(urlObj);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li>prepend(content):向每个匹配的元素的内部的开始处插入内容<br><strong>代码示例:</strong><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">var len = $(&quot;li&quot;).length;</span><br><span class="line">//创建li的jQuery元素</span><br><span class="line">var liObj = $(&quot;&lt;li&gt;列表项&quot;+(len+1)+&quot;&lt;/li&gt;&quot;);</span><br><span class="line">//在ul内追加jQuery的li元素</span><br><span class="line">//$(&quot;ul&quot;).append(liObj);</span><br><span class="line">/* var urlObj = $(&quot;ul&quot;);</span><br><span class="line">liObj.appendTo(urlObj); */</span><br><span class="line">/* $(&quot;ul&quot;).prepend(liObj);</span><br><span class="line"> var urlObj = $(&quot;ul&quot;); */</span><br><span class="line"> var urlObj = $(&quot;ul&quot;);</span><br><span class="line">liObj.prependTo(urlObj);</span><br></pre></td></tr></table></figure></li>
<li>prependTo(content) :将每个匹配的元素插入到指定的元素内部的开始处<br><strong>代码示例:</strong><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">function myclick()&#123;</span><br><span class="line">	var len = $(&quot;li&quot;).length;</span><br><span class="line">	//创建li的jQuery元素</span><br><span class="line">	var liObj = $(&quot;&lt;li&gt;列表项&quot;+(len+1)+&quot;&lt;/li&gt;&quot;);</span><br><span class="line">	//在ul内追加jQuery的li元素</span><br><span class="line">	//$(&quot;ul&quot;).append(liObj);</span><br><span class="line">	/* var urlObj = $(&quot;ul&quot;);</span><br><span class="line">	liObj.appendTo(urlObj); */</span><br><span class="line">	$(&quot;ul&quot;).prepend(liObj);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="JQuery中的DOM操作应用"><a href="#JQuery中的DOM操作应用" class="headerlink" title="JQuery中的DOM操作应用"></a>JQuery中的DOM操作应用</h3><p><img src="/myblog/../../../image/JqueryDomOpera003.png"><br><strong>代码示例:</strong></p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line">/* var myclick = function()&#123;</span><br><span class="line">	alert(&quot;0&quot;);</span><br><span class="line">&#125; */</span><br><span class="line">$(function()&#123;</span><br><span class="line">	</span><br><span class="line">&#125;)</span><br><span class="line">function rightMove()&#123;</span><br><span class="line">	//获得左面被选中的option</span><br><span class="line">	$(&quot;#perm option&quot;).each(function()&#123;</span><br><span class="line">		//获得每一个option的是否被选中的属性值</span><br><span class="line">		var opt = $(this).attr(&quot;selected&quot;);</span><br><span class="line">		if(opt == &quot;selected&quot;)&#123;</span><br><span class="line">			$(&quot;#selPerm&quot;).append($(this));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br><span class="line">function rightMoveAll()&#123;</span><br><span class="line">	//获得左面被选中的option</span><br><span class="line">	$(&quot;#perm option&quot;).each(function()&#123;</span><br><span class="line">		//var copy = $(this).clone();</span><br><span class="line">		$(&quot;#selPerm&quot;).append($(this));</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br><span class="line">function leftMove()&#123;</span><br><span class="line">	//获得左面被选中的option</span><br><span class="line">	$(&quot;#selPerm option&quot;).each(function()&#123;</span><br><span class="line">		//获得每一个option的是否被选中的属性值</span><br><span class="line">		var opt = $(this).attr(&quot;selected&quot;);</span><br><span class="line">		if(opt == &quot;selected&quot;)&#123;</span><br><span class="line">			$(&quot;#perm&quot;).append($(this));</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br><span class="line">function leftMoveAll()&#123;</span><br><span class="line">	//获得左面被选中的option</span><br><span class="line">	$(&quot;#selPerm option&quot;).each(function()&#123;</span><br><span class="line">		$(&quot;#perm&quot;).append($(this));</span><br><span class="line">	&#125;)</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;style type=&quot;text/css&quot;&gt;</span><br><span class="line">select&#123;</span><br><span class="line">	width: 200px;</span><br><span class="line">	height: 200px;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;table&gt;</span><br><span class="line">	&lt;tr&gt;</span><br><span class="line">		&lt;td&gt;</span><br><span class="line">			&lt;select id=&quot;perm&quot; multiple=&quot;multiple&quot;&gt;</span><br><span class="line">				&lt;option&gt;登录&lt;/option&gt;</span><br><span class="line">				&lt;option&gt;员工管理&lt;/option&gt;</span><br><span class="line">				&lt;option&gt;部门管理&lt;/option&gt;</span><br><span class="line">				&lt;option&gt;订单管理&lt;/option&gt;</span><br><span class="line">			&lt;/select&gt;</span><br><span class="line">		&lt;/td&gt;</span><br><span class="line">		&lt;td&gt;</span><br><span class="line">			&lt;input type=&quot;button&quot; value=&quot;&gt;&quot; onclick=&quot;rightMove()&quot;&gt;&lt;br&gt;</span><br><span class="line">			&lt;input type=&quot;button&quot; value=&quot;&gt;&gt;&quot; onclick=&quot;rightMoveAll()&quot;&gt;&lt;br&gt;</span><br><span class="line">			&lt;input type=&quot;button&quot; value=&quot;&amp;lt&quot; onclick=&quot;leftMove()&quot;&gt;&lt;br&gt;</span><br><span class="line">			&lt;input type=&quot;button&quot; value=&quot;&amp;lt&amp;lt&quot; onclick=&quot;leftMoveAll()&quot;&gt;</span><br><span class="line">		&lt;/td&gt;</span><br><span class="line">		&lt;td&gt;</span><br><span class="line">			&lt;select id=&quot;selPerm&quot; multiple=&quot;multiple&quot;&gt;</span><br><span class="line">			&lt;/select&gt;</span><br><span class="line">		&lt;/td&gt;</span><br><span class="line">	&lt;/tr&gt;</span><br><span class="line">&lt;/table&gt;</span><br><span class="line">```                                          </span><br><span class="line">### 外部插入节点</span><br><span class="line">1. after(content) :在每个匹配的元素之后插入内容 </span><br><span class="line">2. before(content):在每个匹配的元素之前插入内容 </span><br><span class="line">**以上方法不但能将新创建的 DOM 元素插入到文档中, 也能对原有的 DOM 元素进行移动.**</span><br><span class="line">### 创建节点</span><br><span class="line">使用 jQuery 的工厂函数 $(): $(html); 会根据传入的 html 标记字符串创建一个 DOM 对象, 并把这个 DOM 对象包装成一个 jQuery 对象返回</span><br><span class="line">**注意**</span><br><span class="line">动态创建的新元素节点不会被自动添加到文档中, 而是需要使用其他方法将其插入到文档中; </span><br><span class="line">当创建单个元素时, 需注意闭合标签和使用标准的 XHTML 格式. 例如创建一个&lt;p&gt;元素, 可以使用 $(“&lt;p/&gt;”) 或 $(“&lt;p&gt;&lt;/p&gt;”), 但不能使用 $(“&lt;p&gt;”) 或 $(“&lt;/P&gt;”)</span><br><span class="line">创建文本节点就是在创建元素节点时直接把文本内容写出来; 创建属性节点也是在创建元素节点时一起创建</span><br><span class="line">### 删除节点</span><br><span class="line">**remove():** </span><br><span class="line">	从 DOM 中删除所有匹配的元素, 传入的参数用于根据 jQuery 表达	式来筛选元素. 当某个节点用 remove() 方法删除后, 该节点所包含的	所有后代节点将被同时删除. 这个方法的返回值是一个指向已被删除	的节点的引用.</span><br><span class="line">**empty():** </span><br><span class="line">	清空节点 – 清空元素中的所有后代节点(不包含属性节点).</span><br><span class="line">### 复制节点</span><br><span class="line">**clone():** </span><br><span class="line">克隆匹配的 DOM 元素, 返回值为克隆后的副本. 但此时复制的新节点不具有任何行为.</span><br><span class="line">**clone(true):**</span><br><span class="line">复制元素的同时也复制元素中的的事件 </span><br><span class="line">**代码示例:**</span><br></pre></td></tr></table></figure>
<script type="text/javascript">
$(function(){
    $("#div1").click(function(){
        alert("div1的点击事件");
    })
})
function copy(){
    var divObj = $("#div1");
    //如果带着true的参数复制就会把事件也复制过去，如果不去指定参数就不会复制事件
    var divObjcopy = divObj.clone(true);
    $("#div2").after(divObjcopy);
}
</script>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">### 属性操作</span><br><span class="line">**attr():** </span><br><span class="line">获取属性和设置属性</span><br><span class="line">+ 当为该方法传递一个参数时, 即为某元素的获取指定属性</span><br><span class="line">+ 当为该方法传递两个参数时, 即为某元素设置指定属性的值</span><br><span class="line">jQuery 中有很多方法都是一个函数实现获取和设置. 如: attr(), html(), text(), val(), height(), width(), css() 等.</span><br><span class="line">**removeAttr():** </span><br><span class="line">删除指定元素的指定属性</span><br><span class="line">### 样式操作</span><br><span class="line">**获取class和设置class:**</span><br><span class="line">class是元素的一个属性,所以获取class和设置class都可以使用attr()方法来完成.</span><br><span class="line">**追加样式:** </span><br><span class="line">addClass() </span><br><span class="line">**移除样式:** </span><br><span class="line">removeClass() 从匹配的元素中删除全部或指定的class</span><br><span class="line">**切换样式:**</span><br><span class="line">toggleClass() 控制样式上的重复切换.如果类名存在则删除它,如果类名不存在则添加它.</span><br><span class="line">**判断是否含有某个样式:** </span><br><span class="line">hasClass() 判断元素中是否含有某个class,如果有,则返回true;否则返回false</span><br><span class="line">### 设置获取HTML,文本和值</span><br><span class="line">**读取和设置某个元素中的HTML内容:**</span><br><span class="line">html() 该方法可以用于 XHTML, 但不能用于 XML 文档</span><br><span class="line">**读取和设置某个元素中的文本内容:**</span><br><span class="line">text() 该方法既可以用于 XHTML 也可以用于 XML 文档.</span><br><span class="line">**读取和设置某个元素中的值:**</span><br><span class="line">val() 该方法类似 JavaScript 中的 value 属性. 对于文本框, 下拉列表框, 单选框该方法可返回元素的值(多选框只能返回第一个值).如果为多选下拉列表框, 则返回一个包含所有选择值的数组</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">### 常用的遍历节点方法</span><br><span class="line">**取得匹配元素的所有子元素组成的集合:** </span><br><span class="line">children(). 该方法只考虑子元素而不考虑任何后代元素.</span><br><span class="line">**取得匹配元素后面紧邻的同辈元素的集合:**</span><br><span class="line">next(); </span><br><span class="line">**取得匹配元素前面紧邻的同辈元素的集合:**</span><br><span class="line">prev()</span><br><span class="line">**取得匹配元素前后所有的同辈元素:** </span><br><span class="line">siblings()</span><br><span class="line">**代码示例**</span><br></pre></td></tr></table></figure>
<script type="text/javascript">
function myclick(){
    /* $("ul li").each(function(){
        alert($(this).html())
    }) */
    //获得ul下的孩子节点
    var lis = $("ul").children();
    lis.each(function(){
        alert($(this).html())
    })
}
function myclick1(){
    //siblings获得兄弟节点
    $("div p:first-child").siblings().each(function(){
        alert($(this).html())
    })
}
function login(){
    //根据id选择器获得用户名的jQuery对象
    var unameObj = $("#username");
    var val = unameObj.val();
    var reg = unameObj.attr("reg");
    var tip = unameObj.attr("tip");
    var regObj = new RegExp(reg);
    if(!regObj.test(val)){
        //获得username紧挨着的下一个dom的jQuery对象
        var spanObj = unameObj.next("input");
        spanObj.html(tip);
    }else{
        //提交表单
        $("#myform").submit();
    }
}
</script>
</head>
<body>
<ul>
    <li>列表项1</li>
    <li>列表项2</li>
    <li>列表项3</li>
    <li>列表项4</li>
</ul>
<input type="button" value="点击" onclick="myclick()">
<hr>
<form id="myform" action="${path }/jsp2/tab.jsp" method="post">
    <p>
        用户名：<input id="username" name="username" type="text" reg="^\w{6,8}$" tip="请输入6到8位的单词字符"><span></span>
    </p>
    <p>
        <input type="button" value="登录" onclick="login()">
    </p>
</form>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">### 事件</span><br><span class="line"></span><br><span class="line">1. 事件结合选择器会把选择器选出来的一组元素都加上事件</span><br><span class="line">2. 在元素上可以不去指定事件</span><br><span class="line">3. 事件的方法建议使用匿名方法</span><br><span class="line">4. 事件的赋值必须要放在jQuery的主函数内部</span><br><span class="line"></span><br><span class="line">提供可以绑定的事件有:</span><br><span class="line"></span><br><span class="line">-|-|-|- </span><br><span class="line">:--:|:--:|:--:|:--:</span><br><span class="line">blur|focus|focusin|focusout</span><br><span class="line">load|resize|scroll|unload</span><br><span class="line">click|dblclick|mousedown|mouseup</span><br><span class="line">mousemove|mouseover|mouseout|mouseenter</span><br><span class="line">mouseleave|change|select|submit</span><br><span class="line">keydown|keypress|keyup|error</span><br><span class="line"></span><br><span class="line">**代码示例:**</span><br></pre></td></tr></table></figure>
<script type="text/javascript">
$(function(){
    /* 1.事件结合选择器会把选择器选出来的一组元素都加上事件
       2.在元素上可以不去指定事件
       3.事件的方法建议使用匿名方法
       4.事件的赋值必须要放在jQuery的主函数内部
    */
    //离开焦点事件
    $("form input").blur(function(){
        //离开焦点的input是哪个这个this就是哪个对象
        var obj = $(this);
        var val = obj.val();
        var reg = obj.attr("reg");
        var tip = obj.attr("tip");
        var regObj = new RegExp(reg);
        if(!regObj.test(val)){
            //获得username紧挨着的下一个dom的jQuery对象
            var spanObj = obj.next();
            spanObj.html("<font color='red'>"+tip+"</font>");
        }else{
            var spanObj = obj.next();
            spanObj.html("");
        }
    });
    //阻止表单提交事件
    $("#myform").submit(function(){
        var isSubmit =true;
        $("form input").each(function(){
            var obj = $(this);
            var val = obj.val();
            var reg = obj.attr("reg");
            var tip = obj.attr("tip");
            var regObj = new RegExp(reg);
            if(!regObj.test(val)){
                //获得username紧挨着的下一个dom的jQuery对象
                var spanObj = obj.next();
                spanObj.html("<font color='red'>"+tip+"</font>");
                isSubmit =false;
            }else{
                var spanObj = obj.next();
                spanObj.html("");
            }
        })
        //阻止表单提交，如果是true就允许表单提交
        return isSubmit;
    });
    //Keydown 按键事件
    $("body").keydown(function(event){
        var num = window.event?event.keyCode : event.which;
        if(num == 13){
            var isSubmit = true;
            $("form input").each(function(){
                var obj = $(this);
                var val = obj.val();
                var reg = obj.attr("reg");
                var tip = obj.attr("tip");
                var regObj = new RegExp(reg);
                if(!regObj.test(val)){
                    //获得username紧挨着的下一个dom的jQuery对象
                    var spanObj = obj.next();
                    spanObj.html("<font color='red'>"+tip+"</font>");
                    isSubmit =false;
                    //跳出each循环
                    //return false;
                }else{
                    var spanObj = obj.next();
                    spanObj.html("");
                }
            })
            if(isSubmit){
                //表单提交
                $("#myform").submit();
            }
        }
    });
})
</script>
</head>
<body>
<form action="">
<p>
    用户名：<input name="username" type="text" reg="^\d{3,5}$" tip="请输入3到5位的数字"><span></span>
</p>
<p>
    密码：<input name="password" type="password" reg="^\w{6,8}$" tip="请输入6到8位的英文或者数字或_"><span></span>
</p>
<p>
    地址：<input name="address" type="text" reg="^.{0,50}$" tip="请输入50个字符"><span></span>
</p>
</form>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">### ajax</span><br><span class="line">type：请求方式get或者是post</span><br><span class="line">url：请求地址</span><br><span class="line">data：发送的数据</span><br><span class="line">success是回调的数据方法</span><br><span class="line">dataType：返回的数据类型json，text，xml，html</span><br><span class="line">error：发生错误时</span><br></pre></td></tr></table></figure>
function ajaxInvoke(){
    var uname = $("#username").val();
    $.ajax({
        type:'post',//请求方式
        url:'${path}/login',//请求的地址
        async:false,//同步的ajax
        data:{
            username:uname//username=uname
        },
        dataType:'json',//返回值的类型，一般使用两种，text，json
        success:function(responseJSON){//ajax调用成功后的回调方法
            alert(1);
            //如果dataType是json那么要求后台一定要返回JSONObject或者JSONArray对象,这个后台的json对象会直接转换成js中的json
            //alert(responseJSON.id+ "  "+responseJSON.name +"   "+responseJSON.age);
            for(var i = 0; i < responseJSON.length; i++){
                alert(responseJSON[i].id + "  "+ responseJSON[i].name + "  "+responseJSON[i].age);
            }
        },
        error:function(){//调用失败进入的方法
            alert("系统错误");//如果404或者后台报异常就会走入error
        }
    });
    //默认情况下ajax是异步的，所以程序不会等着回调函数执行完毕采取执行
    alert(2);
}
```</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://zhangbo4881820.gitee.io/myblog">brad 随风起舞</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhangbo4881820.gitee.io/myblog/posts/63260/">https://zhangbo4881820.gitee.io/myblog/posts/63260/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhangbo4881820.gitee.io/myblog" target="_blank">静待花开</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/myblog/tags/%E5%89%8D%E7%AB%AF%E7%9F%A5%E8%AF%86/">前端知识</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/myblog/posts/22612/" title="git代码上传至指定仓库"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">git代码上传至指定仓库</div></div></a></div><div class="next-post pull-right"><a href="/myblog/posts/62385/" title="readis学习"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">readis学习</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/myblog/posts/20082/" title="CSS01"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-12-16</div><div class="title">CSS01</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/myblog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">brad 随风起舞</div><div class="author-info__description">有些事情需要一辈子的沉淀</div></div><div class="card-info-data site-data is-center"><a href="/myblog/archives/"><div class="headline">文章</div><div class="length-num">57</div></a><a href="/myblog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a><a href="/myblog/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/xxxxx" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:xxxxxx@gmail.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JQuery%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">1.</span> <span class="toc-text">JQuery是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JQuery%E7%9A%84%E5%BC%95%E5%85%A5"><span class="toc-number">2.</span> <span class="toc-text">JQuery的引入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8A%A0%E8%BD%BD%E4%B8%BB%E5%87%BD%E6%95%B0"><span class="toc-number">2.1.</span> <span class="toc-text">加载主函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">2.1.1.</span> <span class="toc-text">第一种方式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="toc-number">2.1.2.</span> <span class="toc-text">第二种方式</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AFJQuery%E5%AF%B9%E8%B1%A1"><span class="toc-number">3.</span> <span class="toc-text">什么是JQuery对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JQuery%E5%AF%B9%E8%B1%A1%E5%92%8CDOM%E5%AF%B9%E8%B1%A1%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="toc-number">3.1.</span> <span class="toc-text">JQuery对象和DOM对象之间的转换</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JQuery%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.</span> <span class="toc-text">JQuery选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.1.</span> <span class="toc-text">基本选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%82%E6%AC%A1%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.2.</span> <span class="toc-text">层次选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.</span> <span class="toc-text">过滤选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.1.</span> <span class="toc-text">基本过滤选择器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.2.</span> <span class="toc-text">属性过滤选择器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%90%E5%85%83%E7%B4%A0%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.3.</span> <span class="toc-text">子元素过滤选择器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A8%E5%8D%95%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7%E8%BF%87%E6%BB%A4%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.4.</span> <span class="toc-text">表单对象属性过滤选择器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%A8%E5%8D%95%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">4.3.5.</span> <span class="toc-text">表单选择器</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JQuery%E4%B8%AD%E7%9A%84DOM%E6%93%8D%E4%BD%9C"><span class="toc-number">5.</span> <span class="toc-text">JQuery中的DOM操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JQuery%E4%B8%AD%E7%9A%84DOM%E6%93%8D%E4%BD%9C%E5%BA%94%E7%94%A8"><span class="toc-number">5.1.</span> <span class="toc-text">JQuery中的DOM操作应用</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/15695/" title="jenkins（1）">jenkins（1）</a><time datetime="2023-09-14T02:17:44.000Z" title="发表于 2023-09-14 10:17:44">2023-09-14</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/22012/" title="wsl的Ubuntu子系统（1）">wsl的Ubuntu子系统（1）</a><time datetime="2023-09-13T08:42:19.000Z" title="发表于 2023-09-13 16:42:19">2023-09-13</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/30302/" title="effective-java-(接口优于抽象类)">effective-java-(接口优于抽象类)</a><time datetime="2023-09-12T08:12:48.000Z" title="发表于 2023-09-12 16:12:48">2023-09-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/9473/" title="effective-java(创建和销毁对象--组合的形式优于继承)">effective-java(创建和销毁对象--组合的形式优于继承)</a><time datetime="2023-09-11T08:23:19.000Z" title="发表于 2023-09-11 16:23:19">2023-09-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/myblog/posts/16107/" title="Hello World">Hello World</a><time datetime="2023-09-07T09:44:01.910Z" title="发表于 2023-09-07 17:44:01">2023-09-07</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By brad 随风起舞</div><div class="framework-info"><span>框架 </span><a href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/myblog/js/utils.js"></script><script src="/myblog/js/main.js"></script><script src="/myblog/js/fancybox.umd.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/myblog/js/search/local-search.js"></script></div></div></body></html>